import com.mochimedia.ui.*;

/**
* ...
* @author $(DefaultUser)
*/

class com.mochimedia.ui.Slider extends Cell {
    
    private var _type:String = "slider";
    
    private var _orientation:Number;
    
    private var _backing:BButton;
    private var _dragger:BButton;
    
    private var _value:Number = 0;
    
    public function get value ():Number { 
        
        if (_snap > 0) return Math.round(_value * _snap) / _snap;
        else return _value; 
    
    }
    
    public function set value (value:Number):Void {
        
        value = Math.min(1, Math.max(0, value));
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) _value = _dragger.valueX = value;
        else _value = _dragger.valueY = value;
        
    }
    
    private var _ratio:Number = 1;
    public function get ratio():Number { return _ratio; }
    public function set ratio (r:Number) {
        
        _ratio = Math.min(1, Math.max(0, r));
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) _dragger.resize(Math.ceil(_width * _ratio), _height);
        else _dragger.resize(_width, Math.ceil(_height * _ratio));
        
    }
    
    
    
    private var _snap:Number = 0;
    
    //
    //
    public function Slider (container:MovieClip, width:Number, height:Number, orientation:Number, snap:Number, position:Position, style:Style) {
        
        init(container, width, height, orientation, snap, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, width:Number, height:Number, orientation:Number, snap:Number, position:Position, style:Style):Void {
        
        super.init(container, _width, _height, false, false, 0, position, style);
        
        _orientation = orientation;
        _width = width;
        _height = height;
        _snap = (!isNaN(snap)) ? snap : _snap;
		
		_style = _style.clone( { gradient: false } );

    }
    
    //
    //
    public function create ():Void {
        
        super.create();

        var backingStyle:Style = _style.clone();
        backingStyle.border = false;
        backingStyle.borderWidth = 0;
        backingStyle.round = 0;
        backingStyle.backgroundColor = _style.buttonColor;
        backingStyle.backgroundAlpha = 50;
        
        _backing = BButton(addChild(new BButton(null, "", null, _width, _height, false, false, true, null, backingStyle)));
        _backing.addEventListener(EVENT_PRESS, this, onSliderClick);

        var draggerStyle:Style = _style.clone();
        draggerStyle.borderWidth = 2;
        draggerStyle.borderColor = ColorTools.getTintedColor(_style.buttonColor, _style.backgroundColor, 0.75);
        draggerStyle.backgroundColor = _style.buttonColor;
        
        _dragger = BButton(addChild(new BButton(null, "", null, _width, _height, false, false, true, null, draggerStyle)));
        _dragger.addEventListener(EVENT_CHANGE, this, onSliderMove);
        _dragger.addEventListener(EVENT_RELEASE, this, onSliderRelease);

    }
    
    //
    //
    public function onSliderClick (e:Event):Void {
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) {
        
            if (_mc._xmouse < _dragger.x) _dragger.valueX -= _dragger.width / width;
            else if (_mc._xmouse > _dragger.x + _dragger.width) _dragger.valueX += _dragger.width / width;
            
            _value = _dragger.valueX;
            
        } else {
            
            if (_mc._ymouse < _dragger.y) _dragger.valueY -= _dragger.height / height;
            else if (_mc._ymouse > _dragger.y + _dragger.height) _dragger.valueY += _dragger.height / height;
             
            _value = _dragger.valueY;
            
        }
        
        dispatchEvent(EVENT_CHANGE);
        
    }
    
    //
    //
    public function onSliderMove (e:Event) {
        
        if (_orientation == Position.ORIENTATION_HORIZONTAL) _value = BButton(e.target).valueX;
        else _value = BButton(e.target).valueY;
        
        dispatchEvent(EVENT_CHANGE);
        
    }
    
    //
    //
    public function onSliderRelease (e:Event) {

        if (_snap > 0) value = Math.round(_value * _snap) / _snap;
        
    }
    
}